/***********************************************************
 *
 * Gaoth
 * Copyright (c) 2011- Marcus Lundgren
 * 
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 * 
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it freely,
 * subject to the following restrictions:
 *
 *  1. The origin of this software must not be misrepresented; you must not
 *     claim that you wrote the original software. If you use this software
 *     in a product, an acknowledgment in the product documentation would be
 *     appreciated but is not required.
 *
 *  2. Altered source versions must be plainly marked as such,
 *     and must not be misrepresented as being the original software.
 *
 *  3. This notice may not be removed or altered from any source distribution.
 *
 ***********************************************************/

#include <SFML/System.hpp>
#include "game.h"

Game::Game()
{
    _model = new Model();
    _net_man = new NetworkManager( _model );
    _net_man_thread = new NetworkManagerThread( _net_man );
    _window = new GameWindow( _model, _net_man );
}

Game::~Game()
{
    _net_man_thread->terminate();

    delete _net_man;
    delete _window;
    delete _model;
}

void Game::start()
{
    sf::Clock clock;

    _net_man_thread->launch();

    float time = 0;
    float current_time = 0;
    float time_between_updates = 0.05f;

    while( 1 )
    {
        //sf::Sleep(.1f);
    	time = clock.restart().asSeconds();
    	current_time += time;
    	if( current_time < time_between_updates )
    		continue;

    	current_time = 0.f;

        // TODO
        // INPUT
        // UPDATE
        if( !_window->update( time ) )
            break;
        // GRAPHICS
        _window->draw();
    }
}
